package com.yh.controller;


import com.github.pagehelper.Page;
import com.yh.entity.Books;
import com.yh.service.IBooksService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class BookController {
    @Getter
    @Setter
    @Autowired
    private IBooksService booksService;

    @RequestMapping("/showBooks")
    public Map show(int pageSize, int pageNum, Books books) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("books", books);
        Page page = booksService.getLikeAllByX(map);
        map.put("page", page);
        map.put("totaPage", page.getPages());
        return map;
    }
/*    @RequestMapping("/showBooks")
    public Map show() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", 5);
        map.put("pageNum", 1);
        Books books = new Books();
        books.setBname("");
        map.put("books", books);
        Page page = booksService.getLikeAllByX(map);
        map.put("page", page);
        map.put("totaPage", page.getPages());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("aa",map);
        return map;
    }*/

    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("list");
    }

    @RequestMapping(value = "/update{bid}",method = RequestMethod.GET)
    public ModelAndView update(@PathVariable Integer bid,HttpSession session) {
        Books books=new Books();
        books.setBid(bid);
      books=booksService.getBooksById(books);
      session.setAttribute("book",books);
    return new ModelAndView("redirect:/book_edit");
    }


    @RequestMapping(value = "/update")
    public void updatebook(Books books) {
        booksService.updateBooks(books);
    }
    @RequestMapping(value = "/delbook")
    public void delbook(Books books) {
        booksService.delBooks(books);
    }

    @RequestMapping("/book_edit")
    public ModelAndView edit() {
        return new ModelAndView("edit");
    }

}
